package com.qijian.flowable.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qijian.common.annotation.PlugAnn;
import com.qijian.common.annotation.PlugParam;
import com.qijian.common.core.domain.entity.FlowableSuccess;
import com.qijian.flowable.domain.FlowableRecord;
import com.qijian.flowable.domain.query.FlowableRecordQuery;
import com.qijian.flowable.domain.vo.ExecRecordVo;
import com.qijian.flowable.mapper.FlowableRecordMapper;
import com.qijian.flowable.service.IFlowableRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 流程记录Service业务层处理
 *
 * @author qijian
 * @date 2021-09-17
 */
@Service
@RequiredArgsConstructor
public class FlowableRecordServiceImpl extends ServiceImpl<FlowableRecordMapper, FlowableRecord>
        implements IFlowableRecordService {

    private final FlowableRecordMapper flowableRecordMapper;

    @Override
    @PlugAnn(groupClass = FlowableSuccess.Approval.class)
    @Transactional(rollbackFor = Exception.class)
    public void updateByProcessIntaneId(@PlugParam String processInstanceId, @PlugParam String variables, @PlugParam String nickName, @PlugParam Long userId) {

    }

    @Override
    public Page<FlowableRecord> pageVo(Page<FlowableRecord> page, QueryWrapper<FlowableRecordQuery> queryWrapper) {
        Page<FlowableRecord> flowableRecordPage = flowableRecordMapper.selectVo(page, queryWrapper);
        if (CollectionUtil.isNotEmpty(flowableRecordPage.getRecords())) {
            flowableRecordPage.getRecords().stream().forEach(e->{
                JSONObject jsonObject = JSON.parseObject(e.getBusinessValues());
                if (jsonObject != null) {
                    e.setDeptName(jsonObject.get("deptName") == null ? null : jsonObject.get("deptName").toString());
                    e.setNickName(jsonObject.get("userName") == null ? null : jsonObject.get("userName").toString());
                }
            });
        }
        return flowableRecordPage;
    }

    @Override
    public Page<FlowableRecord> pageVoNew(Page<FlowableRecord> page, QueryWrapper<FlowableRecordQuery> queryWrapper, FlowableRecordQuery query) {
        Page<FlowableRecord> flowableRecordPage = flowableRecordMapper.selectVoNew(page, queryWrapper, query);
        if (CollectionUtil.isNotEmpty(flowableRecordPage.getRecords())) {
            flowableRecordPage.getRecords().stream().forEach(e->{
                JSONObject jsonObject = JSON.parseObject(e.getBusinessValues());
                if (jsonObject != null) {
                    e.setDeptName(jsonObject.get("deptName") == null ? null : jsonObject.get("deptName").toString());
                    e.setNickName(jsonObject.get("userName") == null ? null : jsonObject.get("userName").toString());
                }
            });
        }
        return flowableRecordPage;
    }

    @Override
    public void execRecord(ExecRecordVo vo) {
        flowableRecordMapper.execRecord(vo);
    }

    @Override
    public List<FlowableRecord> getHisRecord(String processInstanceId) {
        return flowableRecordMapper.getHisRecord(processInstanceId);
    }

    @Override
    public int updateStatusByBack(String processInstanceId) {
        return flowableRecordMapper.updateStatusByBack(processInstanceId);
    }

    @Override
    public String getProjectNoList(Long id, String processDefinitionKey) {
        if ("flow_process_performance".equals(processDefinitionKey)) {
            return baseMapper.getProjectNoListProcess(id);
        }
        if ("flow_process_craft_dispatch".equals(processDefinitionKey)) {
            return baseMapper.getProjectNoListCraft(id);
        }
        return baseMapper.getProjectNoListStr(id);
    }

    @Override
    public int updateStatusByReStart(FlowableRecord record) {
        return flowableRecordMapper.updateStatusByReStart(record);
    }
}
